// 引入公共localStorage类
import StorageModel from "@/utils/storage";
// api
import api, {logout} from '@/api'
import router from '../router'
import store from '../store'
// cookie
import Cookie from "js-cookie";
import * as filters from '../filters'
// 引入全局mixins
import GlobalMixins from '@/mixins'
// 引入所有constant
import * as Constant from '@/utils/constant'

const vHeader = () => import('@/components/layout/header')
const vContent = () => import('@/components/layout/content')
const navBar = () => import("@/components/layout/navbar");

export default {
  install: (Vue) => {
    Vue.component("vHeader", vHeader)
    Vue.component("vContent", vContent)
    Vue.component("navBar", navBar)
    Object.keys(filters).forEach(key => Vue.filter(key, filters[key]))

    Vue.prototype.$logout = async () => {
      await api.logout()
      logout()
      store.commit('global/changeUserInfo', {data: null})
      setTimeout(() => {
        router.push({name: '/'})
      }, 1000)
    }
    // 把常量挂载到vue上
    Vue.prototype.$constant = Constant
    Vue.prototype.$Cookie = Cookie
    // 注册全局mixins
    Vue.mixin(GlobalMixins)
    Vue.prototype.$isIos = () => {
      let u = navigator.userAgent;
      let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      return isiOS
    }

    Vue.prototype.$Storage = new StorageModel();
    Vue.prototype.$SesStorage = new StorageModel("sessionStorage");
    localStorage.setItem("WEB_TITLE", process.env.VUE_APP_TITLE)
    Vue.prototype.$formatNumber =  (val) => {
      return parseInt(val);
    }
    Vue.prototype.$deepClone = (obj) => {
      let _obj = JSON.stringify(obj);
      let objClone = JSON.parse(_obj);
      return objClone;
    }
    // 唯一id
    Vue.prototype.$genId = () => {
      return `id-${Date.now()}-${Math.random().toString(16).substr(2)}`;
    }
  }
}
